<?php

namespace app\admin\controller\mall\goods;

use app\common\controller\Backend;

/**
 * 属性关联
 *
 * @icon fa fa-circle-o
 */
class Goodsattr extends Backend
{
    
    /**
     * Goodsattr模型对象
     * @var \app\common\model\mall\goods\Goodsattr
     */
    protected $model = null;
    protected $goods_id = null;
    protected $cat_ids = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\goods\Goodsattr;
        $this->goods_id = $this->request->param('goods_id');
        $this->assignconfig('goods_id', $this->goods_id);
        //获取商品的分类IDS
        $this->cat_ids = $this->request->param('cat_ids');
        $this->assignconfig('cat_ids', $this->cat_ids);

    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->alias("ga")
                ->join('__MALL_GOODS_CATATTR__ ca', "ga.attr_id=ca.id","RIGHT")
                ->where(["ca.cat_id"=>["in", $this->cat_ids],"ca.showis"=>1])
                ->where($where)
                ->count();

            $list = $this->model
                ->alias("ga")
                ->join('__MALL_GOODS_CATATTR__ ca', "ga.attr_id=ca.id","RIGHT")
                ->where(["ca.cat_id"=>["in", $this->cat_ids],"ca.showis"=>1])
                ->where($where)
                ->field("ca.attrname,ca.attrtype,ca.options,ga.*")
                ->order("ga.attr_id,ga.id")
                ->order($sort, $order)
                ->select();
            $list = collection($list)->toArray();

            //添加未设置的分类属性行
            $attrs = db('mall_goods_catattr')
                ->where(["cat_id"=>["in", $this->cat_ids],"showis"=>1])
                ->select();
            $attrIds = array_column($list,'attr_id');//已设置的规格ID
            $attrIds = array_unique($attrIds);
            foreach ($attrs as $attr) {
                //未设置的规格ID
                if( !in_array($attr["id"],$attrIds) ){
                    $tempRow = [];
                    $tempRow['goods_id'] = $this->goods_id;
                    $tempRow['attr_id'] = $attr['id'];
                    $tempRow['attrval']= "此规格未设置，请根据需要添加！";
                    $tempRow['goods_id_text'] = $this->model->getGoodsIdTextAttr($this->goods_id,[]);
                    $tempRow['attr_id_text'] = $this->model->getAttrIdTextAttr($attr['id'],[]);
                    $list[] = $tempRow;
                    $total++;
                }
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

}
